package cn.edu.cug.cs.gtl.apps.ui;


import cn.edu.cug.cs.gtl.apis.ui.actions.*;
import cn.edu.cug.cs.gtl.apis.ui.spacex.SpaceXApp;
import cn.edu.cug.cs.gtl.renders.openinventor.FactoryImpl;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.IntelliJTheme;
import org.pf4j.DefaultPluginManager;
import org.pf4j.PluginManager;

import javax.swing.*;
import java.io.*;
import java.util.List;

public class MainApp extends SpaceXApp {



    public MainApp(){
        mainWindow = new MainWindow(this);
    }

    public MainWindow getMainWindow() {
        return (MainWindow) mainWindow;
    }


//   public Render getRender() {
//       return this.getMainWindow().getRenderPanel().getRender();
//   }
//
//
//   public Viewer getViewer() {
//       return this.getMainWindow().getRenderPanel().getViewer();
//   }

    public static void main(String [] args){


        //https://blog.csdn.net/qq_36341729/article/details/104092758
        //https://www.formdev.com/flatlaf/themes/
        //https://www.formdev.com/flatlaf/
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //采用本地系统当前风格
            UIManager.setLookAndFeel(new FlatDarculaLaf());
            IntelliJTheme.setup(new FileInputStream(new File("src/main/resources/Xcode-Dark.theme.json")));
            UIManager.getDefaults().put("TabbedPane.showTabSeparators", true);
            UIManager.getDefaults().put("Tree.lineTypeDashed", true);
        } catch( Exception ex ) {
            System.err.println( "Failed to initialize LaF" );
        }


        MainApp theApp = new MainApp();

        theApp.getModel3dUIApp().setFactory(new FactoryImpl());


        theApp.getMainWindow().setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        theApp.getMainWindow().initial();
        theApp.getMainWindow().setSize(1024,768);
        theApp.getMainWindow().pack();

        //显示登录窗口
        LoginDialog loginDialog = new LoginDialog(null);
        loginDialog.setVisible(true);

        // 检查登录是否成功
        if (loginDialog.isLoginSuccess()) {
            // 登录成功，显示主窗口
            theApp.getMainWindow().setVisible(true);
        } else {
            // 登录失败，可以在这里处理失败逻辑，例如不显示主窗口或者重新尝试登录
            // 根据需要，您可以选择让登录对话框保持打开状态，或者关闭它并显示一些指示

        }

        PluginManager manager = new DefaultPluginManager();
        theApp.setPluginManager(manager);

//        //加载3D渲染插件
//        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/render4oi-plugin/target/render4oi-plugin-1.0-SNAPSHOT.zip").toPath());
//        manager.startPlugin("render4oi-plugin");
//        List<Factory> fs = manager.getExtensions(Factory.class);
//        for(Factory f : fs){
//            theApp.factory=f;
//        }


        //加载其他插件

        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/h3dfile-plugin/target/h3dfile-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("h3dfile-plugin");
        List<H3DFileActions> h3dFileActions = manager.getExtensions(H3DFileActions.class);
        for(H3DFileActions f : h3dFileActions){
            f.initialize(theApp);
        }


        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/shpfile-plugin/target/shpfile-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("shpfile-plugin");
        List<SHPFileActions> shpFileActions = manager.getExtensions(SHPFileActions.class);
        for(SHPFileActions f : shpFileActions){
            f.initialize(theApp);
        }

        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/htsfile-plugin/target/htsfile-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("htsfile-plugin");
        List<HTSFileActions> htsFileActions = manager.getExtensions(HTSFileActions.class);
        for(HTSFileActions f : htsFileActions){
            f.initialize(theApp);
        }

        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/catalog-plugin/target/catalog-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("catalog-plugin");
        List<CatalogActions> catalogActions = manager.getExtensions(CatalogActions.class);
        for(CatalogActions c : catalogActions){
            c.initialize(theApp);
        }



        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/modelling-plugin/target/modelling-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("modelling-plugin");
        List<ModellingActions> modellingActions = manager.getExtensions(ModellingActions.class);
        for(ModellingActions m : modellingActions){
            m.initialize(theApp);
        }

//
//        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/hello-plugin/target/hello-plugin-1.0-SNAPSHOT.zip").toPath());
//        manager.startPlugin("hello-plugin");
//        List<HelloActions> helloActions = manager.getExtensions(HelloActions.class);
//        for(HelloActions h : helloActions){
//            h.initialize(theApp);
//        }

        manager.loadPlugin(new File("D:/gitee/geospacex/plugins/device-plugin/target/device-plugin-1.0-SNAPSHOT.zip").toPath());
        manager.startPlugin("device-plugin");
        List<DeviceActions> deviceActions = manager.getExtensions(DeviceActions.class);
        for(DeviceActions d : deviceActions){
            d.initialize(theApp);
        }
    }
}
